package com.cooge.collection.web;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.cooge.collection.deal.Control;
import com.cooge.collection.pojo.CollectionUrl;
import com.cooge.collection.pojo.Episode;
import com.cooge.collection.pojo.EpisodeVideo;
import com.cooge.collection.pojo.Video;
import com.cooge.collection.util.DataFactory;
import com.cooge.collection.util.DomUtil;

public class YouKuConllection {
	
	private CollectionUrl collectionUrl;
	
	private String indexUrl;
	
	private String vurl;
	
	YouKuConllection(CollectionUrl collectionUrl){
		
		this.collectionUrl = collectionUrl;
		this.indexUrl = collectionUrl.getIndexUrl();
		
	}
	
	public CollectionUrl getCollectionUrl() {
		return collectionUrl;
	}

	public void setCollectionUrl(CollectionUrl collectionUrl) {
		this.collectionUrl = collectionUrl;
	}

	public String getIndexUrl() {
		return indexUrl;
	}

	public void setIndexUrl(String indexUrl) {
		this.indexUrl = indexUrl;
	}
	
	public String getVurl() {
		return vurl;
	}

	public void setVurl(String vurl) {
		this.vurl = vurl;
	}

	public List<Video> getbyEpisode(String vurl,String first){
		
		List<Video> vlist = new ArrayList<Video>();
		
		Document doc = null;
		Elements elements = null;
		{
			try {
				String url = vurl.replace("{id}", first);
				System.out.println(url);
				doc = Jsoup.connect(url).get();
				String[] s = { "div", ".item" };
				elements = DomUtil.Domanalysis(doc, s);
				if(elements.size()==0)
				Thread.sleep(1000);
			} catch (Exception e1) {

				e1.printStackTrace();
			}

		}
		while (elements.size() == 0);

		Iterator<Element> eles = elements.iterator();
		while (eles.hasNext()) {
			Element ele = eles.next();
			String[] d = { ".ititle_w", "a" };
			Elements es = DomUtil.Domanalysis(ele, d);
			//System.out.println(es.attr("title") + " " + es.attr("href"));
			String urll = es.attr("href");
			try {
				doc = Jsoup.connect(urll).get();
				
				String p = doc.getElementById("s_baidu1").attr("href");
				p =p.substring(p.lastIndexOf("pic=")+4,p.length());
				System.out.println(p);
				
				Video  v =new Video();
				String title = es.attr("title");
				
				String [] sss = title.split(" ");
				
				v.setTitle(title);
				v.setVideoAdress(doc.getElementById("link2").val());
				v.setUpdatetime(new Timestamp(new Date().getTime()));
				v.setVideoNo(sss[1]);
				v.setWebImg(p);
				vlist.add(v);
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		return vlist;
	}
 
	public Episode getEpisode(String indexUrl){
		Episode episode  =new Episode();
		
		Elements elements = null;
		Document doc = null;
		{
			try {
				System.out.println(indexUrl);
				doc = Jsoup.connect(indexUrl).get();
				
				Element element= doc.getElementById("showInfo");
				
				String[] s = {".baseinfo"};
				elements = DomUtil.Domanalysis(element, s);
				String [] p  = {".thumb","img"};
				Elements eles = DomUtil.Domanalysis(elements, p);
				String poster = eles.attr("src");
				episode.setPoster(poster);
				String title = eles.attr("alt");
				episode.setTitle(title);
				String [] a  = {".area","a"};
				Elements as = DomUtil.Domanalysis(elements, a);
				String area = as.html();
				episode.setArea(area);
				//获取类型
				String [] t  = {".type","a"};
				Elements ts = DomUtil.Domanalysis(elements, t);
				String type = ts.html();
				
				System.out.println(type);
				
				//主演
				String [] h  = {".host","a"};
				Elements hs = DomUtil.Domanalysis(elements,h);
				String host = hs.html();
				episode.setActor(host);
				//播放数量
				String [] r  = {".ratingstar",".rating"};
				Elements rs = DomUtil.Domanalysis(elements,r);
				String playnum = rs.attr("title");
				
				String sss = playnum.substring(playnum.indexOf("有")+1, playnum.indexOf("人顶过")).replace(",","");
				
				
				episode.setUpdatetime(new Timestamp(new Date().getTime()));
				return episode;
			} catch (Exception e1) {

				e1.printStackTrace();
			}

		}
		return null;
	}
	
	public static void main(String[] args) {
		DataFactory.init();
		
		System.out.println("123123");
		List<CollectionUrl> clist = Control.getList();
		CollectionUrl c = clist.get(3);
		
		Episode episode = new Episode();
		
		episode.setTitle(c.getName());
		
		episode = Control.getEpisodeList(episode).get(0);
		
		String url  = c.getUrl();
		
		YouKuConllection y = new YouKuConllection(c);
		List<Video> vlist = y.getbyEpisode(url,"201407");
		
		for(Video v:vlist){
			v.setIntroduction(c.getName());
			v = Control.saveVideo(v);
			EpisodeVideo e = new EpisodeVideo();
			e.setVideoId(v.getId());
			e.setEpisodeId(episode.getId());
			e.setVideoNo(v.getVideoNo());
			Control.save(e);
			
		}
		
		
	//	Episode episode = y.getEpisode(c.getIndexUrl());
	//	Control.saveEpisode(episode);
		
		
	}
}
